// detail.js
var comment = require("../../utils/utils.js")
var that;

Page({
	data: {
		isAdmin: wx.getStorageSync('isAdmin'),
		navigateToIndex: "false"
	},
	onLoad: function (options) {
		that = this;
		that.loadOrder(options.objectId);
		that.setData({
			navigateToIndex: options.navigateToIndex
		});
		getApp().loadSeller(function (seller) {
			that.setData({
				seller: seller
			});
		});
	},
	onUnload: function(){
		that = this;
		//如果是从订单列表过来的，则点下返回按钮，会跳到首页去
		if(that.data.navigateToIndex == "true"){
			wx.reLaunch({
				url: '../../shop/index/index',
	  	})
		}
	},
	loadOrder: function (objectId) {
		// 加载订单详情
		wx.request({
      url: comment.baseUrl + "/wx/orders/queryOrder",
      data: {
				"orderId": objectId
      },
      success: function (order) {
        order = order.data[0];
        that.setData({
          order: order
        })
      }
    });
	},
	contact: function () {
		var telephone = that.data.seller.telephone;
		wx.makePhoneCall({
			phoneNumber: telephone //仅为示例，并非真实的电话号码
		})
	},
	payment: function () {
		// 支付
		wx.showModal({
			title: '确定要支付订单吗？',
			success: function (res) {
				if (res.confirm) {
					// 取消订单
					var order = that.data.order;
					comment.GetRequest("/wx/orders/updateOrderSate",{
						"orderId": order.orderId,
						"state": 1
					},function(result){
						result = result.data.data
						wx.showToast({
							title: '订单已支付',
							success: function () {
								that.setData({
									order: result
								});
							}
						});
					})
				}
			}
		});
	},
	cancel: function () {
		// 取消确认
		wx.showModal({
			title: '确定要取消订单吗？',
			success: function (res) {
				if (res.confirm) {
					// 取消订单
					var order = that.data.order;
					comment.GetRequest("/wx/orders/updateOrderSate",{
						"orderId": order.orderId,
						"state": -1
					},function(result){
						result = result.data.data
						wx.showToast({
							title: '订单已取消',
							success: function () {
								that.setData({
									order: result
								});
							}
						});
					})
				}
			}
		});
	},
	callReceiver: function (e) {
		var telephone = e.currentTarget.dataset.telephone;
		wx.makePhoneCall({
			phoneNumber: telephone //仅为示例，并非真实的电话号码
		})
	},
	send: function () {
		// 取消确认
		wx.showModal({
			title: '确定要派送订单吗？',
			success: function (res) {
				if (res.confirm) {
					// 取消订单
					var order = that.data.order;
					order.set('status', 2);
					order.save().then(function (orderSaved) {
						wx.showToast({
							title: '订单已派送',
							success: function () {
								that.setData({
									order: orderSaved
								});
							}
						});
					})
				}
			}
		});
	}
})